//
//  AudioHelper.swift
//  vr
//
//  Created by 李旭峰 on 2020/7/17.
//

import UIKit
import AVFoundation

class AudioHelper: NSObject {
    //第一种方式，简单的音频播放
        func playSound(audioUrl: NSURL, isAlert: Bool , playFinish: ()->()) {
            // 一. 获取 SystemSoundID
            //   参数1: 文件路径
            //   参数2: SystemSoundID, 指针
            let urlCF = audioUrl as CFURL
            var systemSoundID: SystemSoundID = 0
            AudioServicesCreateSystemSoundID(urlCF, &systemSoundID)
    //        AudioServicesDisposeSystemSoundID(systemSoundID)
            // 二. 开始播放
            if isAlert {
                // 3. 带振动播放, 可以监听播放完成(模拟器不行)
                AudioServicesPlayAlertSound(systemSoundID)
            }else {
                // 3. 不带振动播放, 可以监听播放完成
                AudioServicesPlaySystemSound(systemSoundID)
            }
            playFinish()
        }
        
        //第二种使用AVAudioPlayer播放
        // 获取音频会话
        let session = AVAudioSession.sharedInstance()
        var player: AVAudioPlayer?
        var currentURL : NSURL?
        let playFinish = "playFinsh"
        
        override init() {
            super.init()
            do{
                //  设置会话类别
                try session.setCategory(AVAudioSession.Category.playback)
                //  激活会话
                try session.setActive(true)
            }catch {
                print(error)
                return
            }
        }
        
        //paly music
        func playMusic(filePath: String) {
            guard let url = NSURL(string: filePath) else {
                return//url不存在
            }
            do{
                //AVAudioSessionCategoryPlayback扬声器模式
                try session.setCategory(AVAudioSession.Category.playback)
            }catch {
                print(error)
                return
            }
           //如果播放的音乐与之前的一样，则继续播放
            if currentURL == url {
                player?.play()
                return
            }
            do {
                player = try AVAudioPlayer(data: FileManager.default.contents(atPath: filePath)!)
                currentURL = url
                player?.delegate = self
                player?.prepareToPlay()
                player?.play()
                print("播放成功，文件路径 ==\(url)")
            }catch {
                return
            }
        }
        // 暂停当前歌曲/pause current music
        func pauseCurrentMusic() -> () {
            player?.pause()
        }
        
        // 继续播放当前歌曲/continue to play current music
        func resumeCurrentMusic() -> () {
            player?.play()
        }
    func stopCurrentMusic(){
        player?.stop()
    }
        
        // 播放到指定时间/play music to specified time
    func seekToTime(time: TimeInterval) -> () {
            player?.currentTime = time
        }
        
        
    static func getFormatTime(timeInterval: TimeInterval) -> String {
            let min = Int(timeInterval) / 60
            let sec = Int(timeInterval) % 60
            let timeStr = String(format: "%02d:%02d", min, sec)
            return timeStr
        }
}

extension AudioHelper: AVAudioPlayerDelegate {
    //播放完成后的回调
    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        let filePath = self.currentURL?.absoluteString ?? ""
        self.currentURL = nil
        self.playMusic(filePath: filePath)
    }
}
